/*
  Copyright (c) 2009, Daniel Lerch Hostalot <dlerch@gmail.com>
  All rights reserved.

  Redistribution  and  use  in  source  and  binary  forms,  with  or without 
  modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the  above copyright notice, 
      this list of conditions and the following disclaimer.
    * Redistributions  in  binary  form must reproduce the above copyright 
      notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of the Coelacanth nor the names of its contributors
      may be used to endorse or promote products derived from this software 
      without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
  POSSIBILITY OF SUCH DAMAGE.
*/


#ifndef __COELACANTH_QUEUE_H__
#define __COELACANTH_QUEUE_H__

#include<stdio.h>
#include<stdlib.h>


/*
 * @func: queue(varname, type)
 * @desc: Create a queue variable
 *
 * @param varname: variable name
 * @param type: type of hashtable elements
 * @return: no return value
 */
#define queue(varname, type) \
   typedef struct _##varname##_node \
   { \
      type value; \
      struct _##varname##_node *next; \
   } \
   varname##_node; \
   \
   typedef struct _##varname##_queue \
   { \
      size_t size; \
      varname##_node *head; \
      varname##_node *tail; \
   } \
   varname##_queue; \
   \
   varname##_queue *varname = malloc(sizeof(varname##_queue)); \
   varname->size = 0; \
   varname->head = NULL; \
   varname->tail = NULL; 

/*
 * @func: queue_ptr(varname, type)
 * @desc: Create a queue pointer
 *
 * @param varname: variable name
 * @param type: type of hashtable elements
 * @return: no return value
 */
#define queue_ptr(varname, type) \
   typedef struct _##varname##_node \
   { \
      type value; \
      struct _##varname##_node *next; \
   } \
   varname##_node; \
   \
   typedef struct _##varname##_queue \
   { \
      size_t size; \
      varname##_node *head; \
      varname##_node *tail; \
   } \
   varname##_queue; \
   \
   varname##_queue *varname = NULL;


/*
 * @func: queue_enqueue(varname, newvalue)
 * @desc: Adds value to end
 *
 * @param varname: variable name
 * @param newvalue: value to enqueue
 * @return: no return value
 */
#define queue_enqueue(varname, newvalue) \
   { \
      varname##_node *new_node = NULL; \
      varname##_node *ptr = varname->tail; \
      new_node = malloc(sizeof(varname##_node)); \
      new_node->value = newvalue; \
      new_node->next = NULL; \
      varname->tail = new_node; \
      varname->size++; \
      if(ptr) ptr->next = new_node; \
      if(!varname->head) varname->head = varname->tail; \
   }

/*
 * @func: queue_dequeue(varname)
 * @desc: Remove first element
 *
 * @param varname: variable name
 * @return: no return value
 */
#define queue_dequeue(varname) \
   { \
      if(varname->size>0) \
      { \
         varname->size--; \
         varname##_node *head = varname->head; \
         varname->head = varname->head->next; \
         free(head); \
         \
         if(varname->size==0) \
         { \
            varname->head = NULL;\
            varname->tail = NULL;\
         } \
      } \
   }

/*
 * @func: queue_size(varname)
 * @desc: Get the number of elements in queue
 *
 * @param varname: variable name
 * @return size: queue size
 */
#define queue_size(varname)  varname->size

/*
 * @func: queue_get_head(varname)
 * @desc: Get first element
 *
 * @param varname: variable name
 * @return value: first element
 */
#define queue_get_head(varname) (varname->size>0 ? varname->head->value : 0)


/*
 * @func: queue_get_tail(varname)
 * @desc: Get last element
 *
 * @param varname: variable name
 * @return value: last element
 */
#define queue_get_tail(varname) (varname->size>0 ? varname->tail->value : 0)

/*
 * @func: queue_free(varname)
 * @desc: free memory allocated by queue_*() functions
 *
 * @param varname: variable name
 * @return: no return value
 */
#define queue_free(varname) \
   { \
      varname##_node *prev = NULL; \
      varname##_node *it = NULL; \
      for(it=varname->head; it!=NULL; it=it->next) \
      { \
         if(prev) free(prev); \
         prev = it; \
      } \
      if(prev) free(prev); \
      if(varname) free(varname); \
   }


#endif





